package com.finance.capacity.formulaSet.controller;

import com.finance.base.constant.BaseConstants;
import com.finance.base.exception.BaseException;
import com.finance.capacity.common.capService.ICapOrganizationService;
import com.finance.capacity.formulaSet.bean.pojo.CapDxCoefficient;
import com.finance.capacity.formulaSet.bean.pojo.CapDxCoefficientErr;
import com.finance.capacity.formulaSet.bean.pojo.CapDxCoefficientList;
import com.finance.capacity.formulaSet.capService.IFormulaSetService;
import com.finance.capacity.formulaSet.request.CapDxCoefficientListReq;
import com.finance.framework.controller.BaseController;
import com.finance.framework.controller.Result;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.system.bean.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: gly
 * @Date: Created in 2017-09-20
 */
@Controller
@RequestMapping("/capacity/formula")
public class FormulaSetController extends BaseController {
    @Resource
    private IFormulaSetService formulaSetService;
    @Resource
    private ICapOrganizationService capOrganizationService;

    @RequestMapping("/list")
    public Result list(CapDxCoefficientListReq form) {
        Page page = Page.getInstance(form);
        CapDxCoefficientList capDxCoefficientList = new CapDxCoefficientList();
        capDxCoefficientList.setSiteId(form.getSiteId());
        capDxCoefficientList.setProvinceId(form.getProvinceId());
        capDxCoefficientList.setCityId(form.getCityId());
        if (StringUtils.isNotBlank(form.getKeyWords())) {
            List<String> keyWordsList = new ArrayList<String>();
            List<String> names = Arrays.asList(form.getKeyWords().split("、"));
            for (String orgName : names) {
                List<String> orgCodes = capOrganizationService.getIdByName(orgName);
                if (orgCodes != null && orgCodes.size() > 0) {
                    keyWordsList.addAll(orgCodes);
                } else {
                    orgCodes.add("no_site");
                    keyWordsList.addAll(orgCodes);
                }
            }
            capDxCoefficientList.setKeyWords(keyWordsList);

        }
        page.setParams(capDxCoefficientList);
        PageResultBO<CapDxCoefficientList> resultBO = formulaSetService.formulaSetListByPage(page);

        return getSuccessResult(resultBO);
    }

    /**
     * 修改系数
     *
     * @param coefficients
     * @return
     */
    @RequestMapping("/update")
    public Result update(String coefficients) {
        User user = getAuthUser();
        if (user == null) {
            throw new BaseException(BaseConstants.ERR_UNKONW_USER);
        }
        List<CapDxCoefficientErr> capDxCoefficientErrs = new ArrayList<CapDxCoefficientErr>();
        String[] coefficientStr = coefficients.split(",");
        for (String coefficient : coefficientStr) {
            CapDxCoefficientErr capDxCoefficientErr = new CapDxCoefficientErr();
            String[] coefficientNew = coefficient.split("&");
            if (coefficientNew.length == 4) {
                CapDxCoefficient capDxCoefficient = new CapDxCoefficient();
                capDxCoefficient.setId(Long.valueOf(coefficientNew[0]));
                capDxCoefficient.setCoefficientFirst(new BigDecimal(coefficientNew[1]));
                capDxCoefficient.setCoefficientSecond(new BigDecimal(coefficientNew[2]));
                capDxCoefficient.setCoefficientThird(new BigDecimal(coefficientNew[3]));
                capDxCoefficient.setUpdateUid(user.getId());
                try {
                    formulaSetService.update(capDxCoefficient);
                } catch (Exception e) {
                    String id = coefficientNew[0];
                    if (StringUtils.isNotBlank(id)) {
                        CapDxCoefficient byId = formulaSetService.getById(id);
                        if (byId == null) {
                            capDxCoefficientErr.setId(Long.valueOf(id));
                            capDxCoefficientErr.setMsg("找不到此ID对应的记录");
                        } else {
                            capDxCoefficientErr.setId(Long.valueOf(id));
                            capDxCoefficientErr.setSiteId(byId.getSiteId());
                            capDxCoefficientErr.setSiteName(byId.getSiteName());
                            capDxCoefficientErr.setMsg("数据库更新发生错误");
                        }
                        e.printStackTrace();
                    } else {
                        String id1 = coefficientNew[0];
                        if (StringUtils.isNotBlank(id1)) {
                            CapDxCoefficient byId = formulaSetService.getById(id1);
                            if (byId == null) {
                                capDxCoefficientErr.setId(Long.valueOf(id1));
                                capDxCoefficientErr.setMsg("找不到此ID对应的记录");
                            } else {
                                capDxCoefficientErr.setId(Long.valueOf(id1));
                                capDxCoefficientErr.setSiteId(byId.getSiteId());
                                capDxCoefficientErr.setSiteName(byId.getSiteName());
                                capDxCoefficientErr.setMsg("传参不符合规范");
                            }
                        }
                    }
                    capDxCoefficientErrs.add(capDxCoefficientErr);
                }

            } else {
                capDxCoefficientErr.setId(Long.valueOf(coefficientNew[0]));
                capDxCoefficientErr.setMsg("传参个数不符合规范");
                capDxCoefficientErrs.add(capDxCoefficientErr);
            }
        }
        if (capDxCoefficientErrs != null && capDxCoefficientErrs.size() > 0) {
            return getSuccessResult(capDxCoefficientErrs);
        } else {
            return getSuccessResult(null);
        }
    }
}

